package dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.activity_login;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.R;
import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.activity_tally.HomeActivity;
import dhu.cst.jnlwt.jiangnan181310209liwanting181310302.tally.database.DatabaseHelper;

public class RegisterActivity extends AppCompatActivity {

    private EditText username_register;
    private EditText password1_register;
    private EditText password2_register;
    private Button button_register;

    private DatabaseHelper databaseHelper;
    private SQLiteDatabase sqLiteDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        username_register = findViewById(R.id.username_register);
        password1_register = findViewById(R.id.password1_register);
        password2_register = findViewById(R.id.password2_register);
        button_register = findViewById(R.id.button_register);

        databaseHelper = new DatabaseHelper(RegisterActivity.this);
        sqLiteDatabase = databaseHelper.getReadableDatabase();

        button_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username_input = username_register.getText().toString();
                String password1_input = password1_register.getText().toString();
                String password2_input = password2_register.getText().toString();
                //输入框为空
                if(username_input.equals("") || password1_input.equals("")){
                    Toast.makeText(RegisterActivity.this,"用户名或密码不能为空",Toast.LENGTH_SHORT).show();
                }
                //输入框不为空，两次密码输入不一致
                else if(!password1_input.equals(password2_input)){
                    Toast.makeText(RegisterActivity.this,"两次输入密码不一致",Toast.LENGTH_SHORT).show();
                }
                //输入框不为空，两次密码输入一致
                else{
                    //输入用户名已被注册
                    if(databaseHelper.isRegistered(sqLiteDatabase,username_input)){
                        Toast.makeText(RegisterActivity.this,"用户名已注册",Toast.LENGTH_SHORT).show();
                    }
                    //输入用户名未被注册，将用户添入用户表，跳转至主界面
                    else{
                        String phoneNumber = getIntent().getStringExtra("phone_toRA");
                        databaseHelper.addUser(sqLiteDatabase,username_input, MD5Utils.md5Password(password1_input),phoneNumber);
                        Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent(RegisterActivity.this, HomeActivity.class);
                        intent.putExtra("phone_toHA",phoneNumber);
                        startActivity(intent);
                    }
                }
            }
        });
        Log.d("tag","RegisterActivity create");
    }

    @Override
    public void finish() {
        super.finish();
        Log.d("tag","RegisterLoginActivity finish");
    }
}